package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.dao.AdChannelDao;
import com.heima.admin.service.AdChannelService;
import com.heima.model.common.admin.AdChannel;
import com.heima.model.common.dtos.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;


import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelDao,AdChannel> implements AdChannelService {

    /**分页查询频道
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult fingByPage(ChannelDto dto) {

        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        dto.checkParam();

        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();

        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdChannel::getName,dto.getName());

        }
        IPage<AdChannel> page = new Page<>(dto.getPage(), dto.getSize());
        page(page,wrapper);

        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());

        responseResult.setData(page.getRecords());

        return responseResult;
    }

    /**
     * 新增频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        adChannel.setIsDefault(true);
        adChannel.setCreatedTime(new Date());

        this.save(adChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        }
        AdChannel channel = this.getById(adChannel.getId());
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        this.updateById(adChannel);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);


    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        if (id == null || id == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel channel = this.getById(id);

        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不存在,无法删除");
        }

        if (channel.getStatus()) {
            return  ResponseResult.errorResult(AppHttpCodeEnum.LOGIC_ERROR,"频道启用中,无法删除");
        }
        this.removeById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }
}
